//
//  MessageModel.swift
//  Pods
//
//  Created by eme on 2017/4/10.
//
//

import UIKit
import ModelProtocol
import SwiftyJSON
import NetWorkCore

class MessageModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kMessageTotalKey: String = "total"
    internal let kMessageRowsKey: String = "rows"
    internal let kMessageRecordsKey: String = "records"
    
    
    // MARK: 属性
    var total: Int
    var rows: [RowsModel]
    var records: Int
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        total = json[kMessageTotalKey].intValue
        rows = []
        if let items = json[kMessageRowsKey].array {
            for item in items {
                if let rowsModel = RowsModel(json: item){
                    rows.append(rowsModel)
                }
            }
        } else {
            rows = []
        }
        records = json[kMessageRecordsKey].intValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(total as AnyObject, forKey: kMessageTotalKey)
        
        if rows.count > 0 {
            var temp: [AnyObject] = []
            for item in rows {
                temp.append(item.dictionaryRepresentation()  as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kMessageRowsKey)
        }

        dictionary.updateValue(records as AnyObject, forKey: kMessageRecordsKey)
        
        
        return dictionary
    
    }
    
}
